package com.gbits.websns.kernelsdk.facade.support.entity.impl;

import java.util.Date;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.gbits.websns.kernelsdk.facade.support.entity.IEntity;

@MappedSuperclass
public abstract class BaseEntity extends IdentityEntity implements IEntity {

    public static final String CREATE_DATE_PROPERTY_NAME = "createDate";// "创建日期"属性名称
    public static final String MODIFY_DATE_PROPERTY_NAME = "modifyDate";// "修改日期"属性名称
    public static final String ON_SAVE_METHOD_NAME = "onCreate";// "保存"方法名称
    public static final String ON_UPDATE_METHOD_NAME = "onUpdate";// "更新"方法名称

    @Column(name = "CREATE_DATE", nullable = false,updatable=false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date createDate = new Date();// 创建日期

    @Column(name = "CREATOR_ID")
    private String creatorId;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "MODIFY_DATE", updatable=true)
    private Date modifyDate;// 修改日期

    @Column(name = "MODIFIER_ID")
    private String modifierId;

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public Date getModifyDate() {
        return modifyDate;
    }

    public void setModifyDate(Date modifyDate) {
        this.modifyDate = modifyDate;
    }

    /**
     * @return the creatorId
     */
    public String getCreatorId() {
        return creatorId;
    }

    /**
     * @return the modifierId
     */
    public String getModifierId() {
        return modifierId;
    }

    @PrePersist
    public void onCreate() {
        modifyDate = createDate = new Date();
    }

    @PreUpdate
    public void onUpdate() {
        modifyDate = new Date();
    }

    /**
     * @param creatorId
     *            the creatorId to set
     */
    public void setCreatorId(String creatorId) {
        this.creatorId = creatorId;
    }

    /**
     * @param modifierId
     *            the modifierId to set
     */
    public void setModifierId(String modifierId) {
        this.modifierId = modifierId;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.gbits.websns.kernel.common.entity.IdentityEntity#toMap()
     */
    @Override
    public Map<Object, Object> toMap(boolean cascade) {
        Map<Object, Object> map = super.toMap(cascade);
        map.put("createDate", this.createDate);
        map.put("modifyDate", this.modifyDate);
        map.put("creatorId", this.creatorId);
        map.put("modifierId", this.modifierId);
        return map;
    }
}
